package com.jietuo.class01;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2024/3/1
 * @description: 7，局部最小值问题,无序数组，无相同数字，找出 左边的比它大，右边的比它大 的任意一个数
 */
public class Code07_BSMinLocal {
    public static void main(String[] args) {
        int[] arr = new int[]{11,6, 5, 12, 8, 9, 10};
        int position = getPosition(arr);
        System.out.println(arr[position]);
    }

    public static int getPosition(int[] arr) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        if (arr.length == 1 || arr[0] < arr[1]) {
            return 0;
        }
        if (arr[arr.length - 1] < arr[arr.length - 2]) {
            return arr.length - 1;
        }
        int left = 1;
        int right = arr.length - 2;
        while (left < right) {
            int mid = (left + right)/2;
            if (arr[mid] > arr[mid - 1]) {
                right = mid - 1;
            } else if (arr[mid] > arr[mid + 1]) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return left;
    }
}
